package com.jxd.project.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxd.project.report.dao.IReportDao;
import com.jxd.project.report.dto.SelectReportDto;
import com.jxd.project.report.model.Report;
import com.jxd.project.report.service.IReportService;
import com.jxd.project.report.vo.ReportWithEname;
import com.jxd.project.report.vo.WorkLoadByMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReportServiceImpl
 * @Description TODO
 * @Author 孙福桐
 * @Date 2025/10/1 23:18
 * @Version 1.0
 */
@Service
public class ReportServiceImpl extends ServiceImpl<IReportDao, Report> implements IReportService {
    @Autowired
    private IReportDao reportDao;

    /**
     * 漏报日期获取
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getLostReport(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int empno = Integer.parseInt(queryMap.get("empno"));
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (page != null && limit != null) {
            IPage<String> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectLostDate(empno, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 获取部门漏报日期
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getLostReportByDept(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int deptno = Integer.parseInt(queryMap.get("deptno"));
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (page != null && limit != null) {
            IPage<Map<String, Object>> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectLostReportByDept(deptno, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 报工查询
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getAllReport(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        String deptno = queryMap.get("deptno");
        String empno = queryMap.get("empno");
        String ename = queryMap.get("ename");
        String date = queryMap.get("date");
        String state = queryMap.get("state");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");

        if (page != null && limit != null) {
            IPage<ReportWithEname> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            SelectReportDto selectReportDto = new SelectReportDto(deptno, empno, ename, date, state);
            iPage = reportDao.selectAllReport(selectReportDto, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);

            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 待审核报工列表
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getCheckReport(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int deptno = Integer.parseInt(queryMap.get("deptno"));
        String date = queryMap.get("date");
        String ename = queryMap.get("ename");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (ename == null) ename = "";

        if (page != null && limit != null) {
            IPage<ReportWithEname> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectCheckReport(deptno, ename, date, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 获取单个报工信息
     * @param reportid
     * @return
     */
    @Override
    public ReportWithEname getOneReport(int reportid) {
        return reportDao.selectOne(reportid);
    }

    /**
     * 退回报工列表
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getReturnReport(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int deptno = Integer.parseInt(queryMap.get("deptno"));
        String empno = queryMap.get("empno");
        String ename = queryMap.get("ename");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (ename == null) ename = "";

        if (page != null && limit != null) {
            IPage<ReportWithEname> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectReturnReport(deptno, ename, empno, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 保存报工信息
     * @param report
     * @return
     */
    @Override
    public boolean saveReport(Report report) {
        return reportDao.insertReport(report);
    }

    /**
     * 批量通过
     * @param reportids
     * @return
     */
    @Override
    public boolean passBatch(List<Integer> reportids) {
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("reportid", reportids)
                .set("state", 3);

        return reportDao.update(null, updateWrapper) > 0;
    }

    /**
     * 批量退回
     * @param reportids
     * @return
     */
    @Override
    public boolean rejectBatch(List<Integer> reportids) {
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("reportid", reportids)
                .set("state", 0);

        return reportDao.update(null, updateWrapper) > 0;
    }

    /**
     * 更新报工信息
     * @param report
     * @return
     */
    @Override
    public boolean updateReport(Report report) {
        return reportDao.updateReport(report);
    }

    /**
     * 获取月度工作量
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getWorkloadByMonth(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int deptno = Integer.parseInt(queryMap.get("deptno"));
        String date = queryMap.get("date");
        String ename = queryMap.get("ename");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (ename == null) ename = "";
        if (page != null && limit != null) {
            IPage<WorkLoadByMonth> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectWorkLoadByMonth(deptno, date, ename, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    /**
     * 获取月度加班时间
     * @param queryMap
     * @return
     */
    @Override
    public Map<String, Object> getOvertimeByMonth(Map<String, String> queryMap) {
        Map<String,Object> result = new HashMap<>();

        int deptno = Integer.parseInt(queryMap.get("deptno"));
        String date = queryMap.get("date");
        String ename = queryMap.get("ename");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        if (ename == null) ename = "";
        if (page != null && limit != null) {
            IPage<WorkLoadByMonth> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            iPage = reportDao.selectOvertimeByMonth(deptno, date, ename, iPage);
            result.put("data", iPage.getRecords());
            result.put("count", iPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    @Override
    public boolean isAbsence(Map<String, String> queryMap) {
        int empno = Integer.parseInt(queryMap.get("empno"));
        String date = queryMap.get("date");
        return reportDao.selectIsAbsence(empno, date).isEmpty();
    }

}
